var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var openHelper = require("helper/open");
var listdata = mm("do_ListData");
var noData = [{"template":1,"labelContent":"该城市没有开发社区!"}];
// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var listView = ui("listView");
// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "社区选择",
	"labelNext" : "确认"
})
header.refreshData();

var excludeCommunityIds = null;
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	excludeCommunityIds = do_Page.getData().excludeCommunityIds+"";//强制转为字符串
	if(excludeCommunityIds!=null && excludeCommunityIds!="undefined" && excludeCommunityIds!=""){
		filterIds = excludeCommunityIds.split(";");
	}
	laodPage();
});
listView.bindItems(listdata);
// 刷新数据
var cityId = null;
var filterIds = [];
var page;
function laodPage() {
	page = 1;
	var params = {
		page : page,
		cityId : cityId
	};
	if(filterIds.length > 0){
		params.filterIds = filterIds;
	}
	qqHttp.post(communityUrl.url.base.list, params, function(data) {
		var communityList = data.page.content;
		listView.rebound();
		listdata.removeAll();
		if(communityList && communityList.length>0){
			listdata.addData(communityList);
		}else{
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}

function laodNextPage() {
	page ++;
	var params = {
		page : page,
		cityId : cityId
	};
	if(filterIds.length > 0){
		params.filterIds = filterIds;
	}
	qqHttp.post(communityUrl.url.base.list, params, function(data) {
		var communityList = data.page.content;
		if(communityList && communityList.length>0){
			listdata.addData(communityList);
		}
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
		listView.rebound();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		laodNextPage();
		listView.rebound();
	}
});

do_Page.on("check_change_event", function(d) {
	var alldata = listdata.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].id==d.id){
			alldata[i].checked=d.checked;
			listdata.updateOne(i, alldata[i]);
			break;
		}
	}
})

headerRight.on("touch", function() {
	var communitys=[];
	var communityNames=[];
	var alldata = listdata.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].checked){
			communitys.push(alldata[i].id);
			communityNames.push(alldata[i].name)
		}
	}
	do_App.closePage({"type":"selectCommunitys","communityIds":communitys.join(";"),"communityNames":communityNames.join("、")})
})

//选择城市
ui("do_ALayout_city").on("touch", function() {
	openHelper.open("source://view/area/cityMain.ui");
});

//选择城市 返回
do_Page.on("result", function(data){
	if(data.cityId){
		cityId = data.cityId;
		ui("lableCityName").text = data.cityName;
		laodPage();
	}
});